﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace HttpRequest
{
    public partial class SHA256Form : Form
    {
        public SHA256Form()
        {
            InitializeComponent();
        }

        private void btnGetSign_Click(object sender, EventArgs e)
        {
            /**
             * {
    "msgId":"93b35104-3a43-5216-aeba-6d14747c7448",
    "msgSrc":"EXTERNAL_TEST",
    "msgType":"reverse",
    "responseTimestamp":"2024-10-17 16:27:00",
    "resultCode":"NO_INVOICE",
    "resultMsg":"通过商户订单信息/发票二维码编号/发票代码号码，找不到原发票",
    "srcReserve":"",
    "subList":[
    ],
    "sign":"B416255CB2B9C90B263386A939ECF998DD14938E82BA3A4EE71CA8E3BC2044AE"
}
             */
            try
            {
                var msgId = "93b35104-3a43-5216-aeba-6d14747c7448";
                var msgSrc = "EXTERNAL_TEST";
                var msgType = "reverse";
                var responseTimestamp = "2024-10-17 16:27:00";
                var resultCode = "NO_INVOICE";
                var resultMsg = "通过商户订单信息/发票二维码编号/发票代码号码，找不到原发票";
                var str = $"msgId={HttpUtility.UrlEncode(msgId)}&msgSrc={HttpUtility.UrlEncode(msgSrc)}&msgType=reverse&responseTimestamp={HttpUtility.UrlEncode(responseTimestamp)}&resultCode={HttpUtility.UrlEncode(resultCode)}&resultMsg={HttpUtility.UrlEncode(resultMsg)}";
                //txtSignStr.Text = str;

                var encoding = new UTF8Encoding();
                byte[] keyByte = encoding.GetBytes(txtSignKey.Text.Trim());
                byte[] messageBytes = encoding.GetBytes(txtSignStr.Text);
                using (var hmacsha256 = new HMACSHA256(keyByte))
                {
                    byte[] hashValue = hmacsha256.ComputeHash(messageBytes);
                    txtSign.Text = BitConverter.ToString(hashValue).Replace("-", "").ToUpperInvariant();
                    txtSign.Text +=Environment.NewLine+ BitConverter.ToString(hashValue);
                }

            }
            catch (Exception ex)
            {
                txtSign.Text = ex.Message;
            }


        }

        private void getSignStr_Click(object sender, EventArgs e)
        {
            try
            {
                var msgId = "93b35104-3a43-5216-aeba-6d14747c7448";
                var msgSrc = "EXTERNAL_TEST";
                var msgType = "reverse";
                var responseTimestamp = "2024-10-17 16:27:00";
                var resultCode = "NO_INVOICE";
                var resultMsg = "通过商户订单信息/发票二维码编号/发票代码号码，找不到原发票";
                var str = $"msgId={HttpUtility.UrlEncode(msgId)}&msgSrc={HttpUtility.UrlEncode(msgSrc)}&msgType=reverse&responseTimestamp={HttpUtility.UrlEncode(responseTimestamp)}&resultCode={HttpUtility.UrlEncode(resultCode)}&resultMsg={HttpUtility.UrlEncode(resultMsg)}&subList={HttpUtility.UrlEncode("[]")}";
                var str1 = $"msgId={msgId}&msgSrc={msgSrc}&msgType=reverse&responseTimestamp={responseTimestamp}&resultCode={resultCode}&resultMsg={resultMsg}&subList=[]";
                txtSignStr.Text = str1;

            }
            catch (Exception ex)
            {
                txtSign.Text = ex.Message;
            }
        }
    }
}
